package ext.tianma.doc.util;

import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ptc.core.lwc.client.commands.LWCCommands;
import com.ptc.core.lwc.common.view.AttributeDefinitionReadView;
import com.ptc.core.lwc.common.view.ConstraintDefinitionReadView;
import com.ptc.core.lwc.server.LWCAbstractAttributeTemplate;
import com.ptc.core.lwc.server.cache.db.TypeDefinitionDBService;
import com.ptc.core.meta.common.DiscreteSet;
import com.ptc.core.meta.container.common.impl.DiscreteSetConstraint;
import com.ptc.netmarkets.model.NmOid;

import wt.fc.ObjectIdentifier;
import wt.util.WTException;

/**
 * 获得工艺文件的所有文档小类
 * @author deng
 *
 */

@Controller
@RequestMapping({ "/DCCGetDrawingType" })
public class DCCGetDrawingType {
	public static final String ATTRIBUTE_NAME = "SmallDocType";//文档小类
	public static final String DOC_TYPE = "cn.tianma.ProcessDocuments";//工艺文件
	@RequestMapping(value = "/getDrawingType", method = {RequestMethod.POST, RequestMethod.GET })
	public void getDrawingType(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager
				.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			List<String> list = getTypeLegalValues(DOC_TYPE,ATTRIBUTE_NAME);//获得工艺文件的文档小类
			if(list.size()>0) {
				for(int j =0; j < list.size(); j++) {
	        		JSONObject obInner = new JSONObject();
	        		String str = list.get(j);
	        		obInner.put("researchName", str);
	        		obInner.put("showName", str);
	        		array.put(obInner);
	        	}
				ob.put("data", array);
			}else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	/**
	 * 根据类型和属性获取合法值列表
	 * @return
	 */
	public static List<String> getTypeLegalValues(String typeName, String attrName) throws WTException {
		List<String> valueList = new ArrayList<String>();
		LWCAbstractAttributeTemplate td = TypeDefinitionDBService.newTypeDefinitionDBService().getTypeDefinition(typeName);
		NmOid nmOid = NmOid.newNmOid(ObjectIdentifier.newObjectIdentifier(td.toString()));

		//获取类型中的所有属性定义
		ArrayList<? extends Object> adrvs = LWCCommands.getTypeAttributes(nmOid.toString());
		for(Object object:adrvs) {
			if(object instanceof AttributeDefinitionReadView) {
				AttributeDefinitionReadView adrv = (AttributeDefinitionReadView)object;
				//比较相同的属性定义
				if(adrv.getAttributeTypeIdentifier().getAttributeName().equals(attrName)) {
					//获取所有约束
					Collection<ConstraintDefinitionReadView> constraints = adrv.getAllConstraints();
					Iterator<ConstraintDefinitionReadView> iter = constraints.iterator();
					for(ConstraintDefinitionReadView constraint: constraints) {
						String defClassName = constraint.getRule().getRuleClassname();
						//获取合法值类型
						if(defClassName.equals(DiscreteSetConstraint.class.getName())) {
							Serializable ruleData = constraint.getRuleData();
							if(ruleData!=null && ruleData instanceof DiscreteSet) {
								DiscreteSet set  = (DiscreteSet)ruleData;
								Object[] elements = set.getElements();
								for(Object element:elements) {
									if(element instanceof String) {
										String value = (String)element;
										valueList.add(value);
									}
								}
								break;
							}
						}
					}
					break;
				}
			}
		}
		return valueList;
	}
}
